<template>
	<view>
    <!-- 		<robin-editor class="editor"
            @cancel="hideEditor"
            @save="saveEditor"
            v-model="html"
            :imageUploader="uploadImg"
            :muiltImage="true">
        </robin-editor> -->
    <!-- <editor placeholder=""></editor> -->
    <cu-editor ref="editor" :content="html" :formData="formData" :header="header" :url="uploadUrl"
               @before="onUploadBefore" @save="onSave" @update="onUpdate"></cu-editor>
  </view>
</template>

<script>
import cuEditor from '@/components/cu-editor/cu-editor'
import {handleHtmlImage} from '@/components/cu-editor/util'

export default {
  data() {
    return {
      html: '',
      uploadUrl: 'https://sj.shopec.com.cn/hty-manage-api/upload/files',
      header: {},
      formData: {}
    }
  },
  components: {
    cuEditor
  },
  onLoad() {
    this.html = this.$get('addHtml')
  },
  methods: {
    onUploadBefore: function () {
      this.formData = {
        timestamp: (Date.parse(new Date())) / 1000
      }
    },
    onUpdate(res) {
      res.html = handleHtmlImage(res.html)
      this.html = res.html
    },
    //保存
    onSave(e) {
      let html = e.html
      var containsImage = html.search(/<img /i) >= 0 //内容是否包含图片标签
      let txt = e.text.replace(/(^\s*)|[\r\n]|(\s*$)/g, "") //去掉换行符和两端空格
        html = txt == "" && !containsImage ? txt : html

      this.$put('addHtml', html ? html : '')
      uni.navigateBack({
        delta: 1
      })
    },
    saveEditor(e) {
      console.log(e.html)
      this.$put('addHtml', e.html)
      uni.navigateBack({
        delta: 1
      })
    },
    uploadImg: function (img, callback) {
      //上传图片逻辑,将图片链接传给回调函数
      uni.uploadFile({
													url: 'https://sj.shopec.com.cn/hty-manage-api/upload/files', //post请求的地址
													filePath: img,
													name: 'files',
													success: res => {
														//这里要注意，res.data是个String类型，要转对象的话需要JSON.parse一下
														var obj = JSON.parse(res.data);
														console.log(obj);
														if (obj.code == '1') {
														     callback(obj.data.url)
														}
													},
													fail: () => {
														this.showToast({
															title: '上传失败'
														});
													}
												});
			            },
		}
	}
</script>

<style>

</style>
